package cn.factorybox.item.controller;

import cn.factorybox.common.entity.PageResult;
import cn.factorybox.common.entity.Result;
import cn.factorybox.common.utils.BeanMapUtils;
import cn.factorybox.item.annotation.ControllerLogAnnotation;
import cn.factorybox.item.annotation.Log;
import cn.factorybox.item.enums.OperationType;
import cn.factorybox.item.enums.OperationUnit;
import cn.factorybox.item.service.FactoryDeviceDataService;
import cn.factorybox.model.entity.DeviceData;
import cn.factorybox.item.service.DeviceDataService;
import cn.factorybox.model.entity.FactoryDeviceData;
import cn.factorybox.model.entity.RfidDeviceData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-04 17:43
 */

@Api(tags = "终端设备数据")
@Slf4j
@RestController
@RequestMapping("devicedata")
public class DeviceDataController {

    @Autowired
    private DeviceDataService deviceDataService;

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "接收json数据流")
    @ApiOperation(value = "接收json数据流", notes = "接收json数据流")
    @PostMapping("saveJson")
    public boolean save(@RequestBody DeviceData deviceData) {
        log.info("接收json数据流：{}",deviceData.toString());
        return deviceDataService.saveJson(deviceData);
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "接收RFID数据")
    @ApiOperation(value = "接收RFID数据", notes = "接收RFID数据")
    @PostMapping("saveRfid")
    public boolean saveRfid(@RequestBody RfidDeviceData deviceData) {
        log.info("接收到RFID数据：{}",deviceData.toString());
        return deviceDataService.saveRfid(deviceData);
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "获取数据列表，可用于表格")
    @ApiOperation(value = "数据列表，可用于表格", notes = "数据列表，可用于表格")
    @GetMapping("list")
    public Result selectList(@RequestParam Map<String,Object> map) throws Exception {
        PageResult result = deviceDataService.selectList(map);
        Map<String,Object> maps = BeanMapUtils.beanToMap(result);
        return Result.ok(maps);
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "获取整点数据")
    @ApiOperation(value = "获取整点数据", notes = "获取整点数据")
    @GetMapping("newList")
    public Result getIntegalTime(@RequestParam("deviceID") String fSerialNumber){
        Map<String,Object> map = deviceDataService.getIntegalTime(fSerialNumber);
        Map<String,Object> mapdata = new HashMap<>();
        mapdata.put("data",map);
        return Result.ok(mapdata,"获取整点数据");
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "获取今天和昨天的数据")
    @ApiOperation(value = "获取今天和昨天的数据", notes = "获取今天和昨天的数据")
    @GetMapping("allDayList")
    public Result getAllDayList(@RequestParam(value = "deviceID") String fSerialNumber){
        Map<String,Object> map = deviceDataService.getAllDayList(fSerialNumber);
        return Result.ok(map);
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "获取最新数据")
    @ApiOperation(value = "获取最新数据", notes = "获取最新数据")
    @GetMapping("newData")
    public Result getNewData(@RequestParam("deviceID") String fSerialNumber) throws Exception {
        Map<String,Object> map = deviceDataService.getNewData(fSerialNumber);
        return Result.ok(map);
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "获取一天的异常数据")
    @ApiOperation(value = "获取一天的异常数据", notes = "获取一天的异常数据")
    @GetMapping("warningList")
    public Result getWarningList(@RequestParam("deviceID") String fSerialNumber) {

        Map<String,Integer> map = deviceDataService.getWarningList(fSerialNumber);
        Map<String,Object> maps = new HashMap<>();
        maps.put("data",map);
        return Result.ok(maps);
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "获取室内外数据之差")
    @ApiOperation(value = "获取室内外数据之差", notes = "获取室内外数据之差")
    @GetMapping("differdatas")
    public Result getDifferentData(@RequestParam("deviceID") String fSerialNumber) {
        Map<String,Object> map = deviceDataService.getDifferDatas(fSerialNumber);
        Map<String,Object> maps = new HashMap<>();
        maps.put("data",map);
        return Result.ok(maps,"成功获取室内外差数据");
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "获取当日峰值")
    @ApiOperation(value = "获取当日峰值", notes = "获取当日峰值")
    @GetMapping("peak")
    public Result getCrestValue(@RequestParam("deviceID") String fSerialNumber) {
        Map<String,Object> map = deviceDataService.getCrestValue(fSerialNumber);
        return Result.ok(map,"成功获峰值数据");
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "获取各类型数据各状态信息")
    @ApiOperation(value = "获取各类型数据各状态信息", notes = "获取各类型数据各状态信息")
    @GetMapping("status")
    public Result getDataStatus(@RequestParam("deviceID") String fSerialNumber) {
        Map<String,Object> map = deviceDataService.getDataStatus(fSerialNumber);
        return Result.ok(map,"成功获取信息");
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "获取今日前一百条数据")
    @ApiOperation(value = "获取今日前一百条数据", notes = "获取今日前一百条数据")
    @GetMapping("hundredList")
    public Result getHundredData(@RequestParam("deviceID") String fSerialNumber) {
        Map<String,Object> result = deviceDataService.selectHundredList(fSerialNumber);
        return Result.ok(result,"成功获取最新数据");
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "删除数据")
    @Log(detail = "完成删除环境数据",level = 2,operationUnit = OperationUnit.DEVICEDATA,operationType = OperationType.DELETE)
    @ApiOperation(value = "删除数据", notes = "删除数据")
    @DeleteMapping("delete")
    public Result deleteData(@RequestParam("id") String id,@RequestParam("value") String value) {
        deviceDataService.deleteData(id);
        return Result.ok("成功删除数据");
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "查询周数据")
    @ApiOperation(value = "查询周数据", notes = "查询周数据")
    @GetMapping("week")
    public Result getWeekDatas(@RequestParam("deviceID")String fSerialNumber,@RequestParam("date") String date) throws ParseException {
        Map<String,Object> map = deviceDataService.getWeekDatas(fSerialNumber,date);
        return Result.ok(map,"成功获取最新周数据");
    }

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "查询月数据")
    @ApiOperation(value = "查询月数据", notes = "查询月数据")
    @GetMapping("month")
    public Result getMonthDatas(@RequestParam("deviceID")String fSerialNumber,@RequestParam("date") String date) throws ParseException {
        Map<String,Object> map = deviceDataService.getMonthDatas(fSerialNumber,date);
        return Result.ok(map,"成功获取最新月数据");
    }
}
